﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCPets.Models;
using MVCPets.Helpers;

namespace MVCPets.Controllers
{
  public class ContentEditController : BaseController
  {
    #region Security
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
      base.Initialize(requestContext);

      if (SessionHelper.UserRole <= RoleHelper.GetRoleValue("Contributor")) Return404();
    }
    #endregion

    #region Home
    [HttpGet]
    public ActionResult Home()
    {
      var model = new ContentModel("Home");

      if (model.ID == null) model = new ContentModel { ContentName = "Home" };

      return View(model);
    }

    [HttpPost, ValidateInput(false)]
    public ActionResult Home(ContentModel model)
    {
      if (model.Save()) return RedirectToAction("Index", "Home");
      else return View(model);
    }
    #endregion

    #region About
    [HttpGet]
    public ActionResult About()
    {
      var model = new ContentModel("About");

      if(model.ID == null) model = new ContentModel { ContentName = "About" };

      return View(model);
    }

    [HttpPost, ValidateInput(false)]
    public ActionResult About(ContentModel model)
    {
      if (model.Save()) return RedirectToAction("About", "Home");
      else return View(model);
    }
    #endregion

    #region Contact
    [HttpGet]
    public ActionResult Contact()
    {
      var model = new ContentModel("Contact");

      if(model.ID == null) model = new ContentModel { ContentName = "Contact" };
      
      return View(model);
    }

    [HttpPost, ValidateInput(false)]
    public ActionResult Contact(ContentModel model)
    {
      if (model.Save()) return RedirectToAction("Contact", "Home");
      else return View(model);
    }
    #endregion
  }
}
